package com.tycmc.common.log;

import com.tycmc.common.util.Property;
import com.tycmc.kafka.CreateKafkaProducer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;


public class RecordLog {

	// 是否记录日志的开关。0为不记录，1为记录
	private static final String key = Property.getProperty("log4j.record.log");

	// 日志队列Kafka生产者
	private static KafkaProducer<String, String> producer;

	public RecordLog() {
		if (producer == null) {
			CreateKafkaProducer producer_daily = new CreateKafkaProducer();
			producer = producer_daily.initDaily();
		}
	}

	public void recordAndSendLogNormal(String msg) {
		if ("1".equals(key)) {
			init("1", msg);
		}
	}

	public void recordAndSendLogError(String msg) {
		init("0", msg);
	}

	private void init(String type, String msg) {
		producer.send(new ProducerRecord<>(Property.getProperty("producerDaily.topic"),
				new AppendLog().appendDaily(type, System.currentTimeMillis(), "数据信息:" + msg)));
	}
}
